package com.gears_software.sxdc.dccx.data.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.data.finder.IBankFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.UserBankCardInformation;
import com.gears_software.sxdc.dccx.ui.activity.AddBankCardActivity_;
import com.gears_software.sxdc.dccx.ui.activity.BankCardActivity;
import com.gears_software.sxdc.dccx.ui.util.DES3;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.List;

/**
 * Created by chezhenzhen on 2017/8/28.
 */

public class MyBankAdapter extends BaseSwipeAdapter {
    Context context;
    UserBankCardInformation cardInformation;

    public void setClickListener(IBankClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public IBankClickListener clickListener;

    public interface IBankClickListener {
        public void onClick(String flag);
    }


    public MyBankAdapter(Context context, UserBankCardInformation cardInformation) {
        super();
        this.context = context;
        this.cardInformation = cardInformation;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_view_mybank, null);
        SwipeLayout swipeLayout = (SwipeLayout) v.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
            }
        });
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {
            }
        });
        v.findViewById(R.id.re_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // flag 2 表示删除
                clickListener.onClick("2");
            }
        });
        v.findViewById(R.id.re_bianji).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //flag 1 表示编辑
                clickListener.onClick("1");
            }
        });
        return v;
    }

    @Override
    public void fillValues(int position, View convertView) {
        TextView txt_bank = (TextView) convertView.findViewById(R.id.tv_bank);
        TextView txt_bank_number = (TextView) convertView.findViewById(R.id.tv_bank_card_number);
        TextView txt_bianji_icon = (TextView) convertView.findViewById(R.id.txt_bianji_icon);
        TextView txt_delete_icon = (TextView) convertView.findViewById(R.id.txt_delete_icon);
        txt_bianji_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        txt_delete_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        SimpleDraweeView img_bank_icon = (SimpleDraweeView) convertView.findViewById(R.id.iv_bank_image);
        txt_bank.setText(cardInformation.getBank());

        String cardNumber = cardInformation.getCardno();
        try {
            String card = DES3.decode(cardNumber);
            String number = "**** " + "**** " + "**** " + card.substring(card.length() - 4);
            txt_bank_number.setText(number);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (cardInformation.getBankicon() != null && !cardInformation.getBankicon().equals("")) {
            Uri uri = Uri.parse(cardInformation.getBankicon() + RequestUrl.avatar);
            img_bank_icon.setImageURI(uri);
        }


    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public UserBankCardInformation getItem(int position) {
        return cardInformation;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


}
